<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\api\controller;

use think\exception\ValidateException;
use app\api\BaseController;
use plugins\cms\api\validate\Product as Validate;
use plugins\cms\api\model\Product;
use plugins\cms\api\model\ProductCart;
/**
 * 我的购物车
 */
class UserProductCart extends BaseController
{
    /**
     * 用户权鉴
     */
    public $middleware = [\app\api\middleware\AuthCheck::class];

    /**
     * 列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('cartIndex')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $where[] = ['user_id', '=', $this->request->userInfo->id];
            $search = ['date','keyword'];
            $order  = ['create_time' => 'desc'];
            $count  = ProductCart::where($where)->withSearch($search, $input)->count();
            $data   = ProductCart::where($where)->withSearch($search, $input)->append(['price'])->order($order)->page($input['page'], 10)->select();
            return json(['status' => 'success', 'message' => '获取成功', 'data' => $data, 'count' => $count]);
        }
    }

    /**
     * 加入
     */
    public function save() 
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('cartSave')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $exits = ProductCart::where('user_id', $this->request->userInfo->id)->where('product_sku',$input['product_sku'])->find();
            if ($exits) {
                $exits->number = $exits->number + 1;
                $exits->save();
            } else {
                $product = Product::where('id', $input['id'])->find();
                ProductCart::create([
                    'product_id'    => $product->id,
                    'product_cover' => $product->cover,
                    'product_title' => $product->title,
                    'product_sku'   => $input['product_sku'], 
                    'user_id'       => $this->request->userInfo->id,
                    'number'        => 1,
                    'create_time'   => date('Y-m-d H:i:s'),
                ]);
            }
            return json(['status' => 'success', 'message' => '添加成功']);
        }
    }

    /**
     * 删除
     */
    public function delete() 
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('cartDelete')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            ProductCart::where('user_id', $this->request->userInfo->id)->whereIn('id',$input['ids'])->delete();
            return json(['status' => 'success', 'message' => '取消成功']);
        }
    }

    /**
     * 加减
     */
    public function aas() 
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('cartAas')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            ProductCart::where('user_id', $this->request->userInfo->id)->where('id',$input['id'])->update(['number' => $input['number']]);
            return json(['status' => 'success', 'message' => '修改成功']);
        }
    }
}